/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.server.action.service;

import han.bifang.business.service.ServiceManager;
import han.bifang.common.entity.SoaHttpBinding;
import han.bifang.common.entity.SoaJmsBindingWrapper;
import han.bifang.common.entity.SoaMethodQos;
import han.bifang.common.entity.SoaServiceVersion;
import han.bifang.common.entity.SoaSoapBinding;
import han.bifang.common.entity.SoaVersionQos;
import han.bifang.common.entity.SoaWebServicesMethodWrapper;
import han.bifang.common.entity.SoaWsdlWrapper;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.HttpBinding;
import han.bifang.gwt.client.common.entity.JmsBinding;
import han.bifang.gwt.client.common.entity.JmsBindingWrapper;
import han.bifang.gwt.client.common.entity.MethodQos;
import han.bifang.gwt.client.common.entity.MqJms;
import han.bifang.gwt.client.common.entity.NormalJms;
import han.bifang.gwt.client.common.entity.SoapBinding;
import han.bifang.gwt.client.common.entity.Version;
import han.bifang.gwt.client.common.entity.VersionQos;
import han.bifang.gwt.client.common.entity.WebServicesBinding;
import han.bifang.gwt.client.common.entity.WebServicesMethod;
import han.bifang.gwt.client.common.entity.WebServicesMethodWrapper;
import han.bifang.gwt.client.common.entity.WsdlWrapper;
import han.bifang.gwt.server.action.BifangGWTAction;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

/**
 * 
 * @Author Kent
 */
public class GetVersionDetailAction implements BifangGWTAction {

	@Override
	public ResponseObject execute(RequestObject req,
			HttpServletRequest request, HttpServletResponse response)throws BifangException{
		ResponseObject res = new ResponseObject();
		try {
			String id = (String)req.getRequest();
			ArrayList l = ServiceManager.getVersionDetail(id);
			ArrayList ret = new ArrayList();
			SoaServiceVersion sv = (SoaServiceVersion)l.get(0);
			Version v = new Version();
			BeanUtils.copyProperties(v, sv);
			ret.add(v);
			Object b = l.get(1);
			if(b instanceof SoaHttpBinding){
				HttpBinding h = new HttpBinding();
				BeanUtils.copyProperties(h, b);
				ret.add(h);
			}else if(b instanceof SoaSoapBinding){
				SoapBinding h = new SoapBinding();
				BeanUtils.copyProperties(h, b);
				ret.add(h);
			}else if(b instanceof SoaJmsBindingWrapper){
				SoaJmsBindingWrapper sw = (SoaJmsBindingWrapper)b;
				JmsBindingWrapper w = new JmsBindingWrapper();
				JmsBinding jb = new JmsBinding();
				BeanUtils.copyProperties(jb, sw.getJmsBinding());
				w.setJmsBinding(jb);
				if(jb.getServer().equals("WebSphere MQ")){
					MqJms mq = new MqJms();
					BeanUtils.copyProperties(mq, sw.getInfo());
					w.setInfo(mq);
				}else{
					NormalJms j = new NormalJms();
					BeanUtils.copyProperties(j, sw.getInfo());
					w.setInfo(j);
				}
				
				ret.add(w);
			}else if(b instanceof SoaWsdlWrapper){
				SoaWsdlWrapper sww = (SoaWsdlWrapper)b;
				WsdlWrapper ww = new WsdlWrapper();
				WebServicesBinding wsb = new WebServicesBinding();
				BeanUtils.copyProperties(wsb, sww.getWebServicesBinding());
				ww.setWebServicesBinding(wsb);
				ww.setWsdl(sww.getWsdl().getWsdl());
				ArrayList methods = new ArrayList();
				for(int i = 0 , j = sww.getMethods().size() ; i <  j ; i ++){
					SoaWebServicesMethodWrapper swmw = (SoaWebServicesMethodWrapper)sww.getMethods().get(i);
					WebServicesMethodWrapper wmw = new WebServicesMethodWrapper();
					WebServicesMethod wm = new WebServicesMethod();
					BeanUtils.copyProperties(wm, swmw.getMethod());
					wmw.setMethod(wm);
					
//					MethodParam mp = new MethodParam();
//					BeanUtils.copyProperties(mp, swmw.getParams());
//					wmw.setParams(mp);
					ArrayList<MethodQos> q = new ArrayList<MethodQos>();
					for(int m = 0 , n = swmw.getQos().size(); m < n ; m ++){
						SoaMethodQos smq = (SoaMethodQos)swmw.getQos().get(m);
						MethodQos mq = new MethodQos();
						BeanUtils.copyProperties(mq, smq);
						q.add(mq);
					}
					wmw.setQos(q);
					methods.add(wmw);
					
				}
				ww.setMethods(methods);
				ret.add(ww);
			}
			
			List qos = (List)l.get(2); 
			ArrayList qq = new ArrayList();
			for(int i = 0 , j = qos.size(); i < j ; i ++){
				SoaVersionQos svq = (SoaVersionQos)qos.get(i);
				VersionQos vq = new VersionQos();
				BeanUtils.copyProperties(vq, svq);
				qq.add(vq);
			}
			ret.add(qq);
			
			res.setCode("0");
			res.setResponse(ret);
		} catch (BifangException e) {
			throw e;	
		}catch (Exception e) {
			throw new BifangException(BifangErrorCode.UNKNOWN_ERROR,e.getMessage(),e);		
		}
		return res;
	}

}
